package com.web.oa.utils;


import com.web.oa.pojo.ActiveEmployee;
import com.web.oa.pojo.Employee;
import com.web.oa.service.EmployeeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class ManagerTaskHandler implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {

        //spring容器
        WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        /**从新查询当前用户，再获取当前用户对应的领导*/
        //Employee emp = (Employee) request.getSession().getAttribute(Constants.GLOBLE_USER_SESSION);
        ActiveEmployee activeEmployee = (ActiveEmployee) SecurityUtils.getSubject().getPrincipal();

        EmployeeService employeeService = (EmployeeService) context.getBean("employeeService");
        Employee manager = employeeService.findEmployeeManager(activeEmployee.getManageId());
        //设置个人任务的办理人
        delegateTask.setAssignee(manager.getName());

    }
}